package com.group4.Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.group4.DAO.ForumDAO;
import com.group4.DB.Forum;

@WebServlet( name="ListForum", displayName="ListForum", urlPatterns = {"/forum"})
public class ListForum extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 322375720569304353L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		

		PrintWriter out = response.getWriter();
		List<ForumDAO> forumList = Forum.getAllForum();
		Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
		String json = gson.toJson(forumList);
		out.print(json);
		out.flush();
		
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream
		
		

		PrintWriter out = response.getWriter();
		List<ForumDAO> forumList = Forum.getAllForum();
		Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
		String json = gson.toJson(forumList);
		out.print(json);
		out.flush();
		
		
	}
}
